package com.syq.dao.impl;

import com.syq.bean.Grade;
import com.syq.bean.Menu;
import com.syq.bean.Role;
import com.syq.bean.Users;
import com.syq.dao.RoleDao;
import com.syq.util.DBUtils;
import com.syq.util.StudentEnum;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class RoleDaoImpl extends DBUtils implements RoleDao {
    @Override
    public List<Role> getList() {
        String sql = "select * from role where rolestate=1";
        List<Role> roles = new ArrayList<>();
        try {
            resultSet = query(sql,null);
            while (resultSet.next()) {
                Role role = new Role();
                role.setRoleId(resultSet.getInt("roleid"));
                role.setRoleName(resultSet.getString("rolename"));
                role.setRoleState(resultSet.getInt("rolestate"));
                roles.add(role);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            closeAll();
        }
        return roles;
    }

    @Override
    public List<Role> getRoleList(int pageIndex, int pageSize) {
        List<Role> rolesList=new ArrayList<Role>();
        try {
            String  sql = "select * from role limit ?,?";
            List params = new ArrayList();
            params.add((pageIndex-1)*pageSize);
            params.add(pageSize);
            resultSet = query(sql,params);
            while (resultSet.next()) {
                //取出Role
                Role role = new Role();
                role.setRoleId(resultSet.getInt("roleid"));
                role.setRoleName(resultSet.getString("rolename"));
                role.setRoleState(resultSet.getInt("rolestate"));
                rolesList.add(role);

            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return rolesList;
    }

    @Override
    public int total() {
        int toatal = 0;
        try {
            String  sql = "select count(1) from role ";

            resultSet = query(sql,null);
            while (resultSet.next()) {
                toatal = resultSet.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }

        return toatal;
    }

    @Override
    public int addRole(Role  role) {
        int key = 0;
        try {
            String sql = "insert into role values(null,?,?)";
            List param = new ArrayList();
            param.add(role.getRoleName());
            param.add(role.getRoleState());
            int update = update(sql, param);
            ResultSet generatedKeys = pps.getGeneratedKeys();
            if(generatedKeys.next()) {
                key = generatedKeys.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
        closeAll();
        }
        return key;
    }

    @Override
    public Role findById(int roleId) {
        String sql = "select * from role r,middle m,menu me where r.roleid=m.roleid and m.menuid=me.menuid  and r.roleid=?";
        List params = new ArrayList();
        Role role = new Role();
        List<Menu> menuList = new ArrayList<>();
        try {
            params.add(roleId);
            resultSet = query(sql,params);
            while (resultSet.next()) {
                Menu menu = new Menu();
                role.setRoleId(resultSet.getInt("roleid"));
                role.setRoleName(resultSet.getString("rolename"));
                role.setRoleState(resultSet.getInt("rolestate"));

                menu.setMenuId(resultSet.getInt("menuid"));
                menu.setMenuName(resultSet.getString("menuname"));
                menu.setUpmenuId(resultSet.getInt("upmenuid"));
                menu.setState(resultSet.getInt("state"));
                menu.setDesc(resultSet.getString("desc"));
                menu.setUrl(resultSet.getString("url"));
                menuList.add(menu);

            }
            role.setMenuList(menuList);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return role;
    }

    @Override
    public int updateRole(Role role) {
        int update = 0;
        try {
            String sql = "update role set rolename=?,rolestate=? where roleid="+role.getRoleId();
            List params = new ArrayList();
            params.add(role.getRoleName());
            params.add(role.getRoleState());
            update= update(sql, params);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return update;
    }

    @Override
    public Role findAllById(int roleId) {
        Role role = new Role();
        List menuList = new ArrayList();
        try {
            String sql = "select * from role r,menu m,middle mid where r.roleid=mid.roleid and mid.menuid=m.menuid and r.roleid=?";
            List params = new ArrayList();
            params.add(roleId);
            resultSet = query(sql,params);
            while (resultSet.next()) {
                Menu menu = new Menu();
                role.setRoleId(resultSet.getInt("roleid"));
                role.setRoleName(resultSet.getString("rolename"));
                role.setRoleState(resultSet.getInt("rolestate"));

                menu.setMenuId(resultSet.getInt("menuid"));
                menu.setMenuName(resultSet.getString("menuname"));
                menu.setUpmenuId(resultSet.getInt("upmenuid"));
                menu.setState(resultSet.getInt("state"));
                menu.setDesc(resultSet.getString("desc"));
                menu.setUrl(resultSet.getString("url"));
                menuList.add(menu);
            }
            role.setMenuList(menuList);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }


        return role;
    }

    @Override
    public int deleteRole(int roleId) {
        int count =0;
        try {
            String sql = "delete from role  where roleid=?";
            List params = new ArrayList();
            params.add(roleId);
            count = update(sql,params);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            closeAll();
        }
        return count;
    }

    @Override
    public int startRole(int roleId) {
        int count = 0;
        String sql = "update role set rolestate=1 where roleid=?";
        List params = new ArrayList();
        params.add(roleId);
        count = update(sql,params);
        return count;
    }
}
